package de.markruffer.flashguncalculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	protected int guidenumber = 58;
	protected int camDistance = 8;	// results in 8*0,25 = 2
	protected int flashDistance = 8; // results in 8*0,25 = 2;
	protected boolean flashOffCamera = false;
	protected double aperture = 4.0; 
	
	protected TextView txtPower;
	protected Spinner apertures;
	
	protected void calcBestPower(){
		double value;
		if(flashOffCamera){
			value = 0.5 * 0.25 * (flashDistance + camDistance) * aperture;
		} else {
			value = camDistance * 0.25 * aperture;
		}
		
		if(value <= 0.0) value = 0.1;
		
		boolean tooMuch = true;
		double actual = guidenumber;
		int power = 1;
		while(tooMuch){
			if(Math.abs((actual/2.0)-value) > Math.abs(actual-value) ){
				tooMuch = false;
			}
			else {
				power *= 2;
				actual /= 2.0;
			}
		}
		txtPower.setText("1/" + power);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		final SeekBar sbCam = (SeekBar) findViewById(R.id.sbCam);
		final SeekBar sbFlash = (SeekBar) findViewById(R.id.sbFlash);
		final TextView txtCamDist = (TextView) findViewById(R.id.txtCamDist);
		final TextView txtFlashDist = (TextView) findViewById(R.id.txtFlashDist);
		
		final RadioButton radioOn = (RadioButton) findViewById(R.id.radioOn);
		final RadioButton radioOff= (RadioButton) findViewById(R.id.radioOff);
//		final RadioGroup radioGroup=(RadioGroup) findViewById(R.id.radioGroup1);
		
		final EditText edtGuidenumber = (EditText) findViewById(R.id.editGuidenumber);
		txtPower = (TextView) findViewById(R.id.txtPower);
		
		apertures = (Spinner) findViewById(R.id.spApertures);
		ArrayAdapter<CharSequence> aperturesAdapter = ArrayAdapter.createFromResource(this, R.array.apertures, android.R.layout.simple_spinner_item);
		aperturesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
		apertures.setAdapter(aperturesAdapter);
		apertures.setSelection(4);
		
		edtGuidenumber.setText(""+guidenumber);
		sbCam.setProgress(camDistance);
		txtCamDist.setText(camDistance * 0.25 + "m");
		sbFlash.setProgress(flashDistance);
		txtFlashDist.setText(flashDistance * 0.25 + "m");
		
		radioOn.setSelected(true);
		sbFlash.setEnabled(false);
		
		apertures.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				aperture = Double.valueOf((String) apertures.getAdapter().getItem(arg2));
				calcBestPower();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
		});
		
		radioOff.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if(isChecked){
					sbFlash.setEnabled(true);
					flashOffCamera = true;
					calcBestPower();
				}
				else {
					flashOffCamera = false;
				}
			}
			
		});
		radioOn.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if(isChecked){
					sbFlash.setEnabled(false);
					flashOffCamera = false;
					calcBestPower();
				}
				else {
					flashOffCamera = true;
				}
			}
			
		});
		
		sbCam.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				txtCamDist.setText(progress*0.25 + "m");
				camDistance = progress;
				
				calcBestPower();
			}
		});
		
		sbFlash.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				txtFlashDist.setText(progress*0.25 + "m");
				flashDistance = progress;
				
				calcBestPower();
			}
		});
		
		edtGuidenumber.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if(!hasFocus){
					guidenumber = Integer.valueOf(edtGuidenumber.getText().toString());
					calcBestPower();
				}
			}
		});
		
		calcBestPower();
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
